package com.qiyou.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qiyou.dao.IActivityDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.Activity;

/**
 * 加载活动列表
 */
@WebServlet(name = "ActivityList", urlPatterns = { "/ActivityList" })
public class ActivityListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String userID = request.getParameter("userID");

        String sqlAct = "order by acDate desc limit 25";

        IActivityDao activityDao = DaoFactory.getActivityDao();

        List<Activity> activities = null;

        try {
            if (null != userID) {
                activities = activityDao.getActivities(userID);
                if (null != activities) {
                    request.setAttribute("activities", activities);
                    request.getRequestDispatcher("activitylist.jsp").forward(
                            request, response);
                }
                return;
            }
            activities = activityDao.getActivities(new Object[] { sqlAct });

            if (null != activities) {
                request.setAttribute("activities", activities);
                request.getRequestDispatcher("activitylist.jsp").forward(
                        request, response);
            }
        } catch (SQLException e) {
            response.getWriter()
                    .print("<script  language='javascript'>alert('请求的用户信息有误');history.go(-1);</script>");
        }
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
